package com.example.memo2.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.example.memo2.MainActivity
import com.example.memo2.R
import com.example.memo2.ui.detail.MemoDetailActivity

class ReminderReceiver : BroadcastReceiver() {
    
    companion object {
        const val CHANNEL_ID = "memo_reminder_channel"
        const val NOTIFICATION_ID = 1001
        const val EXTRA_MEMO_ID = "memo_id"
        const val EXTRA_MEMO_TITLE = "memo_title"
        const val EXTRA_MEMO_CONTENT = "memo_content"
    }
    
    override fun onReceive(context: Context, intent: Intent) {
        val memoId = intent.getLongExtra(EXTRA_MEMO_ID, -1L)
        val memoTitle = intent.getStringExtra(EXTRA_MEMO_TITLE) ?: context.getString(R.string.memo_reminder_default_title)
        val memoContent = intent.getStringExtra(EXTRA_MEMO_CONTENT) ?: ""
        
        createNotificationChannel(context)
        showNotification(context, memoId, memoTitle, memoContent)
    }
    
    private fun createNotificationChannel(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                context.getString(R.string.notification_channel_name),
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = context.getString(R.string.notification_channel_description)
                enableLights(true)
                enableVibration(true)
            }
            
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
    
    private fun showNotification(context: Context, memoId: Long, title: String, content: String) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        
        // 点击通知跳转到备忘录详情
        val detailIntent = Intent(context, MemoDetailActivity::class.java).apply {
            putExtra(MemoDetailActivity.EXTRA_MEMO_ID, memoId)
        }
        
        val pendingIntent = PendingIntent.getActivity(
            context, 
            memoId.toInt(), 
            detailIntent, 
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        
        // 构建通知
        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_alarm)
            .setContentTitle(title)
            .setContentText(content)
            .setStyle(NotificationCompat.BigTextStyle().bigText(content))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .build()
        
        notificationManager.notify(NOTIFICATION_ID + memoId.toInt(), notification)
    }
} 